package jdbcmapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Types;

import lotus.domino.*;

/**
 * <p>Title: CCMS/Notes Domino Agents</p>
 * <p>Description: Agents that syncronize data between Notes and MySQL server</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: The Salvation Army</p>
 * @author Ansel Robateau
 * @version 1.0
 */

abstract public class AgentRunnerBase extends NotesThread {
  protected Session m_session;
  protected Database m_database;
  public AgentRunnerBase() {
  }
  public void runNotes(){
    try{
      this.m_session = NotesFactory.createSession();
      this.m_database = this.getDatabase();
      this.execute();
    }catch(Exception e){
      e.printStackTrace();
    }
  }
  protected Session getSession(){
      return this.m_session;
  }
  abstract protected Database getDatabase() throws Exception;
  abstract protected void execute() throws Exception;
  /**
   * LogEvent
   *
   * @param agentname String
   * @param databasename String
   * @param servername String
   * @param event String
   * @param data String
   * @param con Connection
   * @param table String
   * @return void
   * @throws Exception
   */
  public static void LogEvent(String agentname,String databasename,String servername,
                              String event,String data,Connection con,String table) {
    String sql = "insert into " + table + " (agentname,databasename,servername,event,data) values "+
        "(?,?,?,?,?)";
    //String sql2 = "delete from " + table + " where eventtime < ?";
    try{
      PreparedStatement ps = con.prepareStatement(sql);
      if(agentname == null){
        ps.setNull(1,java.sql.Types.CHAR);
      }else{
        ps.setString(1, agentname);
      }
      if(databasename == null){
        ps.setNull(2,Types.CHAR);
      }else{
        ps.setString(2, databasename);
      }
      if(servername == null){
        ps.setNull(3,Types.CHAR);
      }else{
        ps.setString(3, servername);
      }
      ps.setString(4, event);
      ps.setString(5, data);
      ps.executeUpdate();
      ps.close();
    }catch(Exception e){
      //System.out.println(agentname+":"+databasename+":"+servername+":"+event);
      //System.out.println(data);
    }
  }}
